package com.sinux.sshUser.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinux.sshUser.dao.DepartmentDaoI;
import com.sinux.sshUser.dao.RoleDaoI;
import com.sinux.sshUser.dao.UserDaoI;
import com.sinux.sshUser.entity.Department;
import com.sinux.sshUser.entity.Role;
import com.sinux.sshUser.entity.User;
import com.sinux.sshUser.service.UserServiceI;

@Service
public class UserServiceImpl implements UserServiceI {

	@Autowired
	private UserDaoI userDao;
	@Autowired
	private RoleDaoI roleDao;
	@Autowired
	private DepartmentDaoI depatrDao;
	
	
	@Override
	public User findByLogName(String logname) {
		User user=userDao.get("from User t where logname="+logname);
		return user;
	}

	@Override
	public List<User> findAllUserList() {
		List<User> users= userDao.find("from User t where ustate=1");
		return users;
	}
	
	@Override
	public List<User> findUserList(String value) {
		String hql="from User u where u.ustate=1 and (u.uid like '%"+value+"%' or u.uname like '%"+value+"%' or u.logname like '%"+value+"%' or u.phone like '%"+value+"%' or u.email like '%"+value+"%') ";
		List<User> users=userDao.find(hql);
		return users;
	}
	@Override
	public List<User> findUserList(String value,int page, int rows) {
		String hql="from User u where u.ustate=1 and (u.uid like '%"+value+"%' or u.uname like '%"+value+"%' or u.logname like '%"+value+"%' or u.phone like '%"+value+"%' or u.email like '%"+value+"%') ";
		List<User> users=userDao.find(hql,page,rows);
		return users;
	}
	
	@Override
	public List<User> findUserPageing(int page, int rows) {
		String hql="from User u where u.ustate=1";
		List<User> users=userDao.find(hql, page, rows);
		return users;
	}

	@Override
	public User login(User user) {
		String hql="from User u where (u.logname =:logname or u.email=:logname) and u.pwd = :pwd ";
		
		Map<String, Object> params=new HashMap<>();
		params.put("logname", user.getLogname());
		params.put("pwd", user.getPwd());
		
		List<User> list=userDao.find(hql, params);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	

	@Override
	public void reg(User user) throws Exception {
		userDao.save(user);
		// TODO Auto-generated method stub
	
	}

	@Override
	public void add(User user) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public User get(String id) {
		String hql="from User u where u.uid="+id;
		
		return userDao.get(hql);
	}

	@Override
	public void edit(User user) throws Exception {
		// TODO Auto-generated method stub

	}
	
	@Override
	public void upState(String id,int state) {
		String hql="update User u set u.ustate=:ustate where u.uid=:uid  ";
		Map<String, Object> params=new HashMap<>();
		params.put("ustate", state);
		params.put("uid", id);
		userDao.update(hql, params);
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void grant(String ids, User user) {
		// TODO Auto-generated method stub

	}

	@Override
	public void editPwd(User user) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<User> loginCombobox(String q) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean instude(String uid, String[] deid) {
		//1.根据uid获取该用户的部门集合,先清空集合,再添加对象
		User user=userDao.get("from User where uid="+uid);
		Set<Department> departs=user.getDeparts();
		departs.clear();
		List list=new ArrayList<>();
		for(int i=0;i<deid.length;i++){
			//2.根据deid获取部门,
			Department depart=depatrDao.get("from Department where id="+deid[i]);
			list.add(depart);		
		}
		//3.将部门添加到集合中,添加多个
		boolean insTag=departs.addAll(list);
		return insTag;
		
	}

	@Override
	public boolean delstude(String uid, String deid) {
		//1.根据uid获取该用户的部门集合,
		User user=userDao.get("from User where uid="+uid);
		Set<Department> departs=user.getDeparts();
		//2.根据deid获取部门,
		Department depart=depatrDao.get("from Department where id="+deid);
		//3.移除指定对象,
		boolean delTAG=departs.remove(depart);
		return delTAG;
	}

	@Override
	public boolean updateUserRo(String uid, String[] roids) {
		User user=userDao.get("from User where uid="+uid);
		Set<Role> roles=user.getRoles();
		roles.clear();
		List list=new ArrayList<>();
		for(int i=0;i<roids.length;i++){
			//2.根据deid获取部门,
			Role role=roleDao.get("from Role where roid="+roids[i]);
			list.add(role);		
		}
		//3.将部门添加到集合中,添加多个
		boolean insTag=roles.addAll(list);
		return insTag;
	}
	
	
	
	
	
}
